/**
 * 收费统计
 */
<template>
  <div>
    
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
             <el-form-item label="所属科室:" prop="deptOffice">
        <!-- 下拉框 -->
        <el-select size="small" v-model="formInline.officeId" placeholder="所属科室" >
                  
          <el-option v-for="office in officeList" :label="office.name" :value="office.name" :key="office.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <div class="block" size="small">
          <span class="demonstration">订单时间:</span>
          <el-date-picker
            size="small"
            v-model="value2"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            align="right"
          >
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <!-- <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit()"
          >添加</el-button
        > -->
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
    <el-table-column
        prop="id"
        label="序号"
        width="120"
        show-overflow-tooltip
        align="center"
      >
      <template slot-scope="scope">
        {{scope.$index+1}}
      </template>
      </el-table-column>
      <el-table-column
        prop="office"
        label="科室"
        width="150"
        show-overflow-tooltip
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="personNum"
        label="接诊人次"
        width="120"
        show-overflow-tooltip
        align="center"
      >
      </el-table-column>

      <el-table-column
        prop="total"
        label="实际收费"
        width="100"
        show-overflow-tooltip
        align="center"
      >
      </el-table-column>
      
    </el-table>
    <!-- 分页组件 -->
    <!-- <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination> -->

  </div>
</template>

<script>
import axios from "axios";
import { ConfigList, ConfigSave, ConfigDelete } from "../../api/payMG";
import Pagination from "../../components/Pagination";
import { regionData,CodeToText,TextToCode} from 'element-china-area-data';
export default {
  data() {
    return {

        //地址三级联动
        options: regionData,
      // selectedOptions: [],

      //时间段选择
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value1: [new Date(2000, 10, 10, 10, 10), new Date(2000, 10, 11, 10, 10)],
      value2: "",

      //查询后端数据存放的地方
      patient: {
        keywords: "",
        startDate: "",
        endDate: "",
        pageNum: "",
        pageSize:"",
        office:"全科",
      },

      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: "添加",
      sex:[
        {key: "男", value: "男" },
        {key: "女", value:"女" },

      ],
      source:[
        {key: "朋友介绍", value: "朋友介绍" },
        {key: "线上预约", value:"线上预约" },
        {key: "线下上门", value:"线下上门" }
      ],
      maritalStatus:[
        {key: "未婚", value: "未婚" },
        {key: "已婚", value:"已婚" },
        {key: "未知", value:"未知" },
      ],
      education:[
        {key: "博士", value: "博士" },
        {key: "硕士", value:"硕士" },
        {key: "本科", value: "本科" },
        {key: "大专", value:"大专" },
        {key: "高中及以下", value:"高中及以下" },
      ],
      occupation:[
        {key: "军人", value: "军人" },
        {key: "学生", value:"学生" },
        {key: "教师", value: "教师" },
        {key: "医生", value:"医生" },
        {key: "工人", value:"工人" },
        {key: "商人", value:"商人" },
        {key: "司机", value:"司机" },
        {key: "公务员", value:"公务员" },
        {key: "其他", value:"其他" },
      ],
      nation:[
        {key: "汉族", value: "汉族" },
        {key: "蒙古族", value:"蒙古族" },
        {key: "回族", value: "回族" },
        {key: "藏族", value:"藏族" },
        {key: "维吾尔族", value:"维吾尔族" },
        {key: "苗族", value:"苗族" },
        {key: "彝族", value:"彝族" },
        {key: "壮族", value:"壮族" },
        {key: "布依族", value:"布依族" },
        {key: "朝鲜族", value:"朝鲜族" },
        {key: "满族", value:"满族" },
        {key: "侗族", value:"侗族" },
        {key: "瑶族", value:"瑶族" },
        {key: "白族", value:"白族" },
        {key: "土家族", value:"土家族" },
        {key: "高山族", value:"高山族" },
        {key: "其他民族", value:"其他民族" },
      ],
      editForm: {
        id:"",
        name: "",
        idcard: "",
        age: "",
        birthday: "",
        sex: "",
        phoneNumber: "",
        address:"",
        myAddress:[],
        detailAddress:"",
        source: "",
        nation: "",
        maritalStatus: "",
        education: "",
        occupation: "",
        workPlace: "",
        mark:"",
        token: localStorage.getItem("logintoken"),
      },
      // rules表单验证
      rules: {
        name: [{ required: true, message: "请输入患者名称", trigger: "blur" }],
        idcard: [
          { required: true, message: "请输入患者身份证号码", trigger: "blur" },
        ],
        age: [{ required: true, message: "请输入患者年龄", trigger: "blur" }],
        // birthday: [
        //   { required: true, message: "请选择患者的出生年月", trigger: "blur" },
        // ],
        sex: [{ required: true, message: "请选择患者性别", trigger: "blur" }],
        phoneNumber: [
          { required: true, message: "请输入患者电话号码", trigger: "blur" },
        ],
        // source: [
        //   { required: true, message: "请选择患者来源", trigger: "blur" },
        // ],
        // address: [
        //   { required: true, message: "请选择地址", trigger: "blur" },
        // ],
        // detailAddress: [
        //   { required: true, message: "请输入详细地址", trigger: "blur" },
        // ],
        // nation: [
        //   { required: true, message: "请选择患者民族", trigger: "blur" },
        // ],
        // maritalStatus: [
        //   { required: true, message: "请选择患者婚姻状态", trigger: "blur" },
        // ],
        // education: [
        //   { required: true, message: "请选择患者文凭", trigger: "blur" },
        // ],
        // occupation: [
        //   { required: true, message: "请选择患者职业", trigger: "blur" },
        // ],
        // workPlace: [
        //   { required: true, message: "请输入患者工作地址", trigger: "blur" },
        // ],
      },
      formInline: {
        page: 1,
        limit: 10,
        name: "",
        payType: 0,
        partner: "",
        token: localStorage.getItem("logintoken"),
        officeId:"全科",
      },
      // 删除部门
      seletedata: {
        ids: "",
        token: localStorage.getItem("logintoken"),
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
       // 科室列表
      officeList: [],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getpatientlist();
    this.getOfficeList();
    this.search();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    handleChange (value) {
        let data =CodeToText[value[0]]+"-"+CodeToText[value[1]]+"-"+CodeToText[value[2]];
        // this.editForm.address = data;
        this.editForm.address = data;
                    
      },
    getpatientlist() {
      this.patient.pageNum = this.formInline.page;
      this.patient.pageSize = this.formInline.limit;
      axios.post("/api/chargeManagement/select", this.patient).then((res) => {
        this.loading = false;
        this.listData = res.data.data.list;
        // 分页赋值
        this.pageparm.currentPage = this.formInline.page;
        this.pageparm.pageSize = this.formInline.limit;
        this.pageparm.total = res.data.data.total;
      });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      // this.getdata(this.formInline);
      this.getpatientlist();

    },
    // 搜索事件
    search() {
      this.formInline.page = 1;
      if(this.value2!==null&&this.value2!==""){
        this.patient.startDate = this.value2[0];
        this.patient.endDate = this.value2[1];
      }else{
        this.patient.startDate=null;
        this.patient.endDate=null;
      }
      
      this.patient.pageNum = this.formInline.page;
      this.patient.office = this.formInline.officeId;
      axios.post("/api/chargeManagement/select", this.patient)
        .then((res) => {
          this.loading = false;
          // console.log('结果是',res)
          // listData赋值
          let total=0;
          
          for(let i=0;i< res.data.data.list.length;i++){
              total+=res.data.data.list[i].registrationFee;
              total+=(res.data.data.list[i].prescriptionFee)/100

          }
          
          this.listData = [{
           office:this.patient.office,
           personNum: res.data.data.list.length,
           total:total



          }];
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page;
          this.pageparm.pageSize = this.formInline.limit;
          this.pageparm.total = res.data.data.total;
        })
        .catch((err) => {
          // this.loading = false
          // this.$message.error('员工列表加载失败，请稍后再试！')
        });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        //将地址分割
        var addrs = row.address.split("-")
        //转换为地址编码数组
        let addrNo=[];
        addrNo[0] = TextToCode[addrs[0]].code;
        addrNo[1] = TextToCode[addrs[0]][addrs[1]].code;
        addrNo[2] = TextToCode[addrs[0]][addrs[1]][addrs[2]].code;
        this.title = "修改";
        this.editForm.id = row.id;
        this.editForm.name = row.name;
        this.editForm.idcard = row.idcard;
        this.editForm.age = row.age;
        this.editForm.birthday = row.birthday;
        // console.log(row.birthday);
        this.editForm.sex = row.sex;
        this.editForm.phoneNumber = row.phoneNumber;
        this.editForm.myAddress = addrNo;
        this.editForm.detailAddress = row.detailAddress;
        this.editForm.source = row.source;
        this.editForm.nation = row.nation;
        this.editForm.maritalStatus = row.maritalStatus;
        this.editForm.education = row.education;
        this.editForm.occupation = row.occupation;
        this.editForm.workPlace = row.workPlace;
        this.editForm.mark = row.mark;
      } else {
        this.title = "添加";
        this.editForm.name = "";
        this.editForm.idcard = "";
        this.editForm.age = "";
        this.editForm.birthday = "";
        this.editForm.sex = "";
        this.editForm.phoneNumber = "";
        this.editForm.myAddress = "";
        this.editForm.detailAddress = "";
        this.editForm.source = "";
        this.editForm.nation = "";
        this.editForm.maritalStatus = "";
        this.editForm.education = "";
        this.editForm.occupation = "";
        this.editForm.workPlace = "";
        this.editForm.mark = "";
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        // console.log('editForm',this.editForm);
          //判断是新增还是 修改  发送不同的请求
          if (this.title=='添加') {
            if (valid) {
                //新增请求
              this.editForm.birthday.setDate(this.editForm.birthday.getDate()+1);
              axios .post("/api/patient/addOnePatient", this.editForm)
              .then((res) => {
              this.loading = false;
              if (res.data.code==200) {
                alert("新增成功!")
                this.getpatientlist();
              } else {
                alert("新增失败!")
              }
               this.editFormVisible = false;
            })
            .catch((err) => {
              this.editFormVisible = false;
              this.loading = false;
              alert("系统繁忙,稍后再试!")
            });
            }else{
              return false;
            }
            
          }else{
             //修改请求
             this.editForm.birthday.setDate(this.editForm.birthday.getDate()+1);
              axios .post("/api/patient/updateOneById", this.editForm)
              .then((res) => {
              // this.editFormVisible = false;
              this.loading = false;
              if (res.data.code==200) {
                alert("修改成功!")
                this.getpatientlist();
              } else {
                alert("修改失败!")
              }
              this.editFormVisible = false;
            })
            .catch((err) => {
              this.editFormVisible = false;
              this.loading = false;
              alert("系统繁忙,稍后再试!")
            });
          }
        
      });
    },
    // 删除患者
    deletePatient(index, row) {
      // console.log("index", index);
      // console.log("row", row.id);
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //发送请求 根据id删除 患者
          axios
            .post("/api/patient/deleteOne?id=" + row.id)
            .then((res) => {
              this.loading = false;
              console.log("删除的结果是", res);
              this.getpatientlist();
            })
            .catch((err) => {
              // this.loading = false
              // this.$message.error('员工列表加载失败，请稍后再试！')
              alert("删除失败,请联系管理员!");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消删除",
          });
        });
    },
    getOfficeList(){
        axios.post('/api/office/selectAllOffice?pageNum=1&pageSize=1000',{})
        .then(res => {
          this.loading = false 
          this.officeList = res.data.data.list                  
        })
        .catch(err => {
          this.loading = false
          this.$message.error('科室加载失败，请稍后再试！')
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog(formName) {
      this.editFormVisible = false;
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 